{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Density Estimation for a Gaussian mixture\n\n\nPlot the density estimation of a mixture of two Gaussians. Data is\ngenerated from two Gaussians with different centers and covariance\nmatrices.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.colors import LogNorm\nfrom sklearn import mixture\n\nn_samples = 300\n\n# generate random sample, two components\nnp.random.seed(0)\n\n# generate spherical data centered on (20, 20)\nshifted_gaussian = np.random.randn(n_samples, 2) + np.array([20, 20])\n\n# generate zero centered stretched Gaussian data\nC = np.array([[0., -0.7], [3.5, .7]])\nstretched_gaussian = np.dot(np.random.randn(n_samples, 2), C)\n\n# concatenate the two datasets into the final training set\nX_train = np.vstack([shifted_gaussian, stretched_gaussian])\n\n# fit a Gaussian Mixture Model with two components\nclf = mixture.GaussianMixture(n_components=2, covariance_type='full')\nclf.fit(X_train)\n\n# display predicted scores by the model as a contour plot\nx = np.linspace(-20., 30.)\ny = np.linspace(-20., 40.)\nX, Y = np.meshgrid(x, y)\nXX = np.array([X.ravel(), Y.ravel()]).T\nZ = -clf.score_samples(XX)\nZ = Z.reshape(X.shape)\n\nCS = plt.contour(X, Y, Z, norm=LogNorm(vmin=1.0, vmax=1000.0),\n                 levels=np.logspace(0, 3, 10))\nCB = plt.colorbar(CS, shrink=0.8, extend='both')\nplt.scatter(X_train[:, 0], X_train[:, 1], .8)\n\nplt.title('Negative log-likelihood predicted by a GMM')\nplt.axis('tight')\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}